package com.link.base.base.contract.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.contract.dao.mybatis.mapper.ModReceivableMapper;
import com.link.base.base.contract.model.ModReceivable;
import com.link.base.core.basic.util.BigDecimalUtils;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 模具回款
 *
 * @author 王昊
 * @date 2021-03-18 15:54:57
 */
@Service
public class ModReceivableServiceImpl extends BasicServiceImpl<ModReceivable> implements ModReceivableService {

    @Resource
    private ModReceivableMapper    modReceivableMapper;

    @Resource
    private FlowService flowService;

    @Override
    public BasicMapper<ModReceivable> getBasicMapper() {
        return modReceivableMapper;
    }


    /**
     * 更新模具回款计划前，计算相差金额
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public ModReceivable queryById(ModReceivable entity) throws Exception {
        //相差金额 = 应收款金额 - 实收款金额
        ModReceivable modReceivable = modReceivableMapper.queryById(entity);
        BigDecimal needReceiveAmount = BigDecimalUtils.changeNullToZero(modReceivable.getNeedReceiveAmount());
        BigDecimal factReceiveAmount = BigDecimalUtils.changeNullToZero(modReceivable.getFactReceiveAmount());
        modReceivable.setAttr1(String.valueOf(needReceiveAmount.subtract(factReceiveAmount).setScale(2,BigDecimal.ROUND_HALF_UP)));
        return modReceivable;
    }

    /**
     * 模具回款审批
     * @author 王昊
     * @date 9:15 2021/3/22
     * @param entity
     * @return void
     **/
    @Override
    public void modReceiveSubmit(ModReceivable entity) throws Exception {
        ModReceivable modReceivable = modReceivableMapper.queryById(entity);
        String approvalType = "modReceiveSubmit";
        ObjectMapper mapper = new ObjectMapper();
        //把Java对象转化成json字符串
        String approvalDetail = mapper.writeValueAsString(modReceivable);
        //创建模具回款审批流
        flowService.createFlow(modReceivable.getId(),approvalType,null,modReceivable.getPartName(),modReceivable.getId(),approvalDetail);
        //审批状态【新建】->【审批中】
        modReceivable.setApprovalStatus("InApproval");
        modReceivableMapper.update(modReceivable);
    }

    /**
     * 模具回款审批通过
     * @author 王昊
     * @date 9:48 2021/3/22
     * @param flow, arg1, arg2, arg3, arg4, arg5
     * @return void
     **/
    @Override
    public void agreeApproval(Flow flow, String arg1, String arg2, String arg3, String arg4, String arg5) throws Exception {
        Long frameContractId = flow.getFlowObjId();
        ModReceivable modReceivable = new ModReceivable();
        modReceivable.setId(frameContractId);
        ModReceivable modReceivable1 = modReceivableMapper.queryById(modReceivable);
        modReceivable1.setApprovalStatus(arg1);
        modReceivableMapper.update(modReceivable1);
    }

    /**
     * 模具合同审批通过更新回款计划创建人为审批发起人
     * @param entity
     * @throws Exception
     */
    @Override
    public void updateCreatedBy(ModReceivable entity) throws Exception {
        modReceivableMapper.updateCreatedBy(entity);
    }
}
